package com.pulsarwebdevelopment.concordance.plugins.android;

import java.util.List;
import java.util.Map;

import com.pulsarwebdevelopment.concordance.plugins.FileType;
import com.pulsarwebdevelopment.concordance.plugins.Plugin;
import com.pulsarwebdevelopment.concordance.plugins.Plugin.Argument;
import com.pulsarwebdevelopment.concordance.plugins.Plugin.SelectionType;

//import com.pulsarwebdevelopment.concordance.Concordance;
import com.pulsarwebdevelopment.concordance.Concordance.ConcordanceInfo;
import com.pulsarwebdevelopment.concordance.ConcordanceTool;
import com.pulsarwebdevelopment.concordance.R;
import android.app.Activity;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class PluginOptionsActivity extends Activity {

	Plugin plugin;
	List<ConcordanceInfo> concList;
	Map<String, Plugin.Argument> argumentMap;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.plugin_options_screen);
		
		Bundle extras = getIntent().getExtras();
		
		String pluginClassName = extras.getString("pluginClass");
		
		try {
			//instantiate the plugin
			Class<?> pluginClass = Class.forName(pluginClassName);
			this.plugin = (Plugin) pluginClass.newInstance();
			
			//get the available concordances
			this.concList = new ConcordanceTool(new ContextWrapper(this.getBaseContext()).getFilesDir()).getConcordances();
			
			//get the list of parameters
			this.argumentMap = plugin.getParameterList();
			
			//set title
			this.setTitle(this.getTitle() + " : " + this.plugin.getTitle());
			
			//set description text
			TextView descText = (TextView) findViewById(R.id.description_text);
			descText.setText(plugin.getDescription());
			
			//create objects on screen 
			this.createWidgetsFromArgumentMap(argumentMap);
			
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (ClassCastException e) {
			e.printStackTrace();
		}
	}

	public void cancelClick(View button){
		Toast.makeText(getBaseContext(), "Canceled", 100);
		this.finish();
	}
	
	public void continueClick(View button){
		//retrieve the map from the plugin
		//Map<String, Plugin.Argument> argumentMap = plugin.getParameterList();
		//LinearLayout optionsPlaceholder = (LinearLayout) findViewById(R.id.options_placeholder);
		
		for(Map.Entry<String, Plugin.Argument> e : this.argumentMap.entrySet()){
			
			switch(e.getValue().selectType){
				case CheckBoxes:
				
					ListView checkList = (ListView) e.getValue().selectedValue;
					
					CheckBox checked = (CheckBox) checkList.getSelectedItem();
					
					//copy and replace the current argument and set the new selected value 
					e.setValue(e.getValue().copy(null, null, null, checked.getText()));
					
					break;
				
				case ConcordanceList:
					Spinner spinner =  (Spinner) e.getValue().selectedValue;
					
					String selectedItem = spinner.getSelectedItem().toString();
					
					int concIndex = this.concList.indexOf(ConcordanceInfo.getFromUniqueDisplayString(selectedItem));

					//TODO make this actually retrieve the concordance ID
		            //get the ID of concordance that we just selected
		            int concId = this.concList.get(concIndex).id;
		            
		            //copy and replace the current argument and set the new selected value 
		            e.setValue(e.getValue().copy(null, null, null, concId));
					
					break;
					
				/*case DisplayText:
				
					break;*/
				
				case FileBrowser:
					FileBrowser browser = (FileBrowser) e.getValue().selectedValue;
					
					e.setValue(e.getValue().copy(null, null, null, browser.getSelectedFile()));
					
					break;
					
				case RadioButtons:
					
					RadioGroup radioList = (RadioGroup) e.getValue().selectedValue;
					
					RadioButton selected = (RadioButton) radioList.findViewById(radioList.getCheckedRadioButtonId());
					
					//copy and replace the current argument and set the new selected value 
					e.setValue(e.getValue().copy(null, null, null, selected.getText()));
					
					break;
					
				case TextInput:
					
					EditText textBox = (EditText) e.getValue().selectedValue;
					
					String inputText = textBox.getText().toString();
					
					e.setValue(e.getValue().copy(null, null, null, inputText));
			
					Log.d("face", e.getValue().selectedValue != null ? e.getValue().selectedValue.getClass().getCanonicalName() : "null");
					break;
			}
		}

		argumentMap.put("androidCallingActivity", new Argument(SelectionType.Generic, null, null, this));
		argumentMap.put("baseConcDir", new Argument(SelectionType.Generic, null, null, 
													new ContextWrapper(this.getBaseContext()).getFilesDir()));
		
		//findViewById(R.id.continue_button).setEnabled(false);
		button.setEnabled(false);
		
		Toast.makeText(getBaseContext(), "Plugin is running...", Toast.LENGTH_LONG);
		
		this.plugin.run(argumentMap);
		
		Toast.makeText(getBaseContext(), "Plugin has completed...", Toast.LENGTH_LONG);
		
		this.finish();
	}

	private void createWidgetsFromArgumentMap(Map<String, Plugin.Argument> argMap){
		
		LinearLayout optionsPlaceholder = (LinearLayout) findViewById(R.id.options_placeholder);
		
		for(Map.Entry<String, Plugin.Argument> e : argMap.entrySet()){
			
			TextView text = new TextView(optionsPlaceholder.getContext());
			text.setText(e.getValue().displayMessage);
			
			optionsPlaceholder.addView(text);
			
			switch(e.getValue().selectType){
				case CheckBoxes:
				
					ListView checkList = new ListView(optionsPlaceholder.getContext());
					
					CheckBox curCheckBox;
					for(String checkOption : (String[]) e.getValue().selectArgs){
						curCheckBox = new CheckBox(checkList.getContext());
						curCheckBox.setFocusable(false);
						
						curCheckBox.setText(checkOption);
						
						checkList.addView(curCheckBox);
					}
					
					optionsPlaceholder.addView(checkList);
					
					//copy and replace the current argument and set the new selected value as the object used by the GUI 
					e.setValue(e.getValue().copy(null, null, null, checkList));
					
					break;
				
				case ConcordanceList:
					Spinner spinner = new Spinner(optionsPlaceholder.getContext());
					
					//add items to spinner
					ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
							optionsPlaceholder.getContext(), android.R.layout.simple_spinner_item);

					for(ConcordanceInfo dropdownInfo : this.concList){
						adapter.add(dropdownInfo.getUniqueDisplayString()); //set the concordance description
					}
					
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					
					spinner.setAdapter(adapter);
					
					//bind a new listener to take care of the select listener
					//I don't think we need this right now
					//spinner.setOnItemSelectedListener(new ConcDropdownListener(e.getKey())); 
					
					optionsPlaceholder.addView(spinner);
					
					//copy and replace the current argument and set the new selected value as the object used by the GUI 
					e.setValue(e.getValue().copy(null, null, null, spinner));
					
					break;
					
				/*case DisplayText:
				
					break;*/
				
				case FileBrowser:
					FileType type = (FileType) e.getValue().selectArgs[0];
					
					FileBrowser browser = new FileBrowser(optionsPlaceholder.getContext(), type);
					
					optionsPlaceholder.addView(browser);
					
					//copy and replace the current argument and set the new selected value as the object used by the GUI 
					e.setValue(e.getValue().copy(null, null, null, browser));
					
					break;
					
				case RadioButtons:
					
					RadioGroup radioList = new RadioGroup(optionsPlaceholder.getContext());
					
					RadioButton curRadio;
					for(String radioOption : (String[]) e.getValue().selectArgs){
						curRadio = new RadioButton(radioList.getContext());
						curRadio.setText(radioOption);
						radioList.addView(curRadio);
					}
					
					optionsPlaceholder.addView(radioList);
					
					//copy and replace the current argument and set the new selected value as the object used by the GUI 
					e.setValue(e.getValue().copy(null, null, null, radioList));
					
					break;
					
				case TextInput:
					EditText textBox = new EditText(optionsPlaceholder.getContext());
					
					optionsPlaceholder.addView(textBox);
					
					//copy and replace the current argument and set the new selected value as the object used by the GUI 
					e.setValue(e.getValue().copy(null, null, null, textBox));
					
					break;
			}
		}
	}
}
